<?php
class RegionAction extends CommonAction {
    public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Region');
    }
	
	//返回地区名称
	public function getRegionName($id){
		if(!($region = F('region_key_value'))){
			$region = $this->modelObject->getField('id,name');
			F('region_key_value', $region);
		}
		
		return $region[$id];
	}
	
	//根据地区名返回地区id
	public function getRegionId($name){
		if(!($region = F('region_key_value'))){
			$region = $this->modelObject->getField('id,name');
			F('region_key_value', $region);
		}
		
		return array_search($name);
	}
    
	//获得所有省份
	public function getProvince(){
		$province = array();
		if(!($province = F('province'))){
			$province = $this->modelObject->getAll(array('types'=>0), 'id ASC', 'id,name');
			F('province', $province);
		}
		
		return $province;
	}
	
	public function getCity($provinceId = 0){
		if($provinceId){
			if(!($city = F('city_'.$provinceId))){
				$city = $this->modelObject->getAll(array('pid'=>$provinceId, 'types'=>1), 'id ASC','id,name');
				F('city_'.$provinceId, $city);
			}
			return $city;
		}
		return false;	
	}
	
	public function ajaxGetCity(){
		if($provinceId = I('province_id', 0 , 'intval')){
			$city = $this->getCity($provinceId);
			$this->ajaxReturn($city, '城市列表', 1);	
		}else{
			$this->ajaxReturn(0, '返回数据失败', 0);	
		}
	}
}//end class